package com.wzapp.sitposture.common.util;

import com.wzapp.sitposture.common.base.DeviceInfo;
import com.wzapp.sitposture.common.base.HeaderParam;
import com.wzapp.sitposture.common.support.HttpKit;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;

@Slf4j
public class HeaderUtil {

    private static final String CITY_ID = "cityId";
    private static final String DEVICE_INFO = "deviceInfo";

    /**
     * 功能: 获取请求Header头信息 <br/>
     *
     * @author ql
     * @date 2018-01-20 14:20
     * @param request
     * @return HeaderParam
     */
    public static HeaderParam getHeaderParam(HttpServletRequest request) {
        HeaderParam headerParam = new HeaderParam();
        headerParam.setCityId(getCity(request));
        headerParam.setDeviceInfo(getDeviceInfo(request));
        headerParam.setRemoteIp(IPAddressUtil.getClientIpAddress(request));
        setTerminalTypeAndClientApplication(headerParam);
        return headerParam;
    }

    private static void setTerminalTypeAndClientApplication(HeaderParam headerParam) {
        String signOs = headerParam.getOs();
        if("android".equalsIgnoreCase(signOs)){
            headerParam.setTerminalType("APP");
            headerParam.setClientApplication("android");
        }else if ( "ios".equalsIgnoreCase(signOs)){
            headerParam.setTerminalType("APP");
            headerParam.setClientApplication("ios");
        }else if ("pc".equalsIgnoreCase(signOs)){
            headerParam.setTerminalType("PC");
            headerParam.setClientApplication("pc");
        }else if ("m-android".equalsIgnoreCase(signOs)){
            //设置为m 即mobile h5的值
            headerParam.setTerminalType("H5");
            headerParam.setClientApplication("android");
        }else if ("m-ios".equalsIgnoreCase(signOs)){
            //设置为m 即mobile h5的值
            headerParam.setTerminalType("H5");
            headerParam.setClientApplication("ios");
        }else {
//           throw new CustomizedException("public.error","请求signOs参数错误");
        }

    }

    /**
     * 功能: 获取请求Header头信息 <br/>
     *
     * @param request
     * @return HeaderParam
     * @author ql
     * @date 2018-01-20 14:20
     */
    public static void getHeaderParam(HttpServletRequest request, HeaderParam headerParam) {
        headerParam.setCityId(getCity(request));
        headerParam.setDeviceInfo(getDeviceInfo(request));
        headerParam.setRemoteIp(IPAddressUtil.getClientIpAddress(request));
        headerParam.setOs(request.getHeader("os"));

        DeviceInfo deviceInfo = headerParam.getDeviceInfo();
        if (null == deviceInfo) {
            return;
        }

        deviceInfo.setOs(headerParam.getOs());

        setTerminalTypeAndClientApplication(headerParam);
    }

    public static String getUserIdFromPrincipal() {
        if(HttpKit.getRequest() == null){
            return null;
        }
        Principal userPrincipal = HttpKit.getRequest().getUserPrincipal();
        if (null == userPrincipal) {
            return null;
        }
        return userPrincipal.getName();
    }

    public static String getCity(HttpServletRequest request){
        return request.getHeader(CITY_ID);
    }

    public static DeviceInfo getDeviceInfo(HttpServletRequest request) {
        String deviceInfoStr = request.getHeader(DEVICE_INFO);
        return DeviceInfoUtil.decodeDeviceInfo(deviceInfoStr);
    }
}
